<?php
header("Content-type:text/html; charset=utf-8");
require_once ('include/conn.php');
require_once ('system.class.php');
function imgarclist($args, $content, &$smarty){
	$num=$args['num'];
	$channel_id_info=$args['channel_id'];
	$channel_id=explode(",", $args['channel_id']);
	$channel_num=count($channel_id);
	if($num==""){$num=4;}
	$time=time();
	$limit_num=$num-1;
	/*用于计数*/
	$article_num=0;
	/*更新置顶的文章,将过期的文件取消置顶设置*/
	$sql_update_is_top_articles="update article_normal set article_is_top=0 where article_is_top=1 and article_top_time<$time;";
	$result_update_is_top_articles=mysql_query($sql_update_is_top_articles);
	/*获取置顶文件*/
	$sql_get_is_top_articles="select article_id,article_title,article_is_top,article_content,article_modify_time,article_is_extends_url,article_extends_url,article_has_picture,article_picture_path,article_main_channel from article_normal 
	where article_status=1 and article_is_top=1 and article_has_picture=1 and article_main_channel in ($channel_id_info) 
	order by article_modify_time desc limit 0,$limit_num;";
	$result_get_is_top_articles=mysql_query($sql_get_is_top_articles);
	$num_is_top_articles=mysql_num_rows($result_get_is_top_articles);
	if($num_is_top_articles>0){
		while($row_get_is_top_articles=mysql_fetch_row($result_get_is_top_articles)){
			$article_id=$row_get_is_top_articles[0];
			$article_main_channel=$row_get_is_top_articles[9];
			$arr[$article_id]['id']=$row_get_is_top_articles[0];
			$arr[$article_id]['title']=$row_get_is_top_articles[1];
			if($row_get_is_top_articles[5]==1){
				$arr[$article_id]['url']=$row_get_is_top_articles[6];
			}
			else{
				$arr[$article_id]['url']=CIE."/article.php?article_id=$article_id&channel_id=$article_main_channel";	
			}
			$arr[$article_id]['picture_src']=strstr($row_get_is_top_articles[8],"uploads");
			    $text_tmp=base64_decode($row_get_is_top_articles[3]);
				$text_tmp=@strip_tags($text_tmp);
				$text_tmp=str_replace(" ", "", $text_tmp);
				$text_tmp=str_replace("&nbsp;", "", $text_tmp);
		        $text_tmp=str_replace("&rdquo;", "", $text_tmp);
		        $text_tmp=str_replace("&ldquo;", "", $text_tmp);
		        $arr[$article_id]['text']=$text_tmp;
		        $article_num++;
			if($article_num==$num){break;}
		}
	}
	/*如果置顶文件个数不够，选最新更新的文章*/
	if($article_num<$num){
		$new_limit=$num-$article_num;
	$sql_get_new_articles="select article_id,article_title,article_is_top,article_content,article_modify_time,article_is_extends_url,article_extends_url,article_has_picture,article_picture_path,article_main_channel from article_normal 
	 where article_status=1 and article_is_top=0 and article_has_picture=1 and article_main_channel in ($channel_id_info) 
	 order by article_modify_time desc limit 0,$new_limit;";
	$result_get_new_articles=mysql_query($sql_get_new_articles);
	$num_get_new_articles=mysql_num_rows($result_get_new_articles);
	if($num_get_new_articles>0){
	while($row_get_new_articles=mysql_fetch_row($result_get_new_articles)){
		$article_id=$row_get_new_articles[0];
		$article_main_channel=$row_get_new_articles[9];
		$arr[$article_id]['id']=$row_get_new_articles[0];
		$arr[$article_id]['title']=$row_get_new_articles[1];
		$arr[$article_id]['publish_time']=date('Y.n.j',$row_get_new_articles[2]);
		if($row_get_new_articles[4]==1){
			$arr[$article_id]['url']=$row_get_new_articles[5];
		}
		else{
			$arr[$article_id]['url']=CIE."/article.php?article_id=$article_id&channel_id=$article_main_channel";
		}
		$arr[$article_id]['picture_src']=strstr($row_get_new_articles[8],"uploads");
		$text_tmp=base64_decode($row_get_new_articles[3]);
		$text_tmp=strip_tags($text_tmp);
		$text_tmp=str_replace("&nbsp;", "", $text_tmp);
		$text_tmp=str_replace("&rdquo;", "", $text_tmp);
		$text_tmp=str_replace("&ldquo;", "", $text_tmp);
		$arr[$article_id]['text']=$text_tmp;
		//$arr[$article_id]['text']=(strlen($text_tmp)>60)?substr($text_tmp,0,60)."...":$text_tmp;
		$article_num++;
		
		if($article_num==$num){break;}
	}
	}
	}
	if(empty($arr)){
		$arr=null;
	}
	$smarty->assign("imglist",$arr);
	return $content;
}
